//
//  StatusOriginalView.swift
//  weibo
//
//  Created by 陈勇 on 16/2/29.
//  Copyright © 2016年 chenyong. All rights reserved.
//

import UIKit

// 微博头像尺寸
private let CZStatusIconSize = CGSize(width: 36, height: 36)

// 原创微博 - 是 Cell 的子视图
class StatusOriginalView: UIView {
    
    /// 微博视图模型
    var viewModel: StatusViewModel? {
        didSet {
            
            // 设置圆角头像(需要扩展参数)
            iconView.cz_setImageWithURL(
                viewModel?.status.user?.profile_image_url,
                placeholderName: "avatar_default_big",
                size: CZStatusIconSize,
                isCorner: true)
            
            // 姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            
            // 设置图标
            //            if viewModel?.status.user?.mbrank > 0 && viewModel?.status.user?.mbrank < 7 {
            //                memberIconView.image = UIImage(named: "common_icon_membership_level(\(viewModel!.status.user!.mbrank))")
            //            }
            memberIconView.image = viewModel?.userMemberImage
            vipIconView.image = viewModel?.userVipImage

            sourceLabel.text = viewModel?.sourceText
    
            timeLabel.text = viewModel?.createTime?.cz_dateDescription

            contentLabel.text = viewModel?.status.text

            //更新配图视图大小
            //根据是否有配图，决定配图视图顶部的约束参照
            let offset = viewModel?.status.pic_urls?.count > 0 ? CZStatusCellLayout.margin : 0
            picturesView.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(viewModel!.picturesViewSize)
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }
            //设置配图视图类容
            picturesView.pic_urls = viewModel?.status.pic_urls

            //更新视图大小，注意和上面的代码不要调换位置
            picturesView.updateImageSize(viewModel!.picturesViewSize)
           
        }
    }
    
    // MARK: - 构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有控件
    //头像图标
    private let iconView: UIImageView = UIImageView(cz_imageName: "avatar_default_big")
    //姓名标签
    private let nameLabel: UILabel = UILabel(cz_text: "上海传智")
    //会员图标
    private let memberIconView: UIImageView = UIImageView(cz_imageName: "common_icon_membership_level2")
    //VIP 图标
    private let vipIconView: UIImageView = UIImageView(cz_imageName: "avatar_grassroot")
    //时间
    private let timeLabel: UILabel = UILabel(cz_text: "时间", fontSize: 10, color: CZAppearanceTintColor)
    //来源
    private let sourceLabel: UILabel = UILabel(cz_text: "来自上海", fontSize: 10)
    //类容标签
    private let contentLabel: UILabel = UILabel(cz_text: "微博", fontSize: 15)
    //配图视图
    private let picturesView: StatusPicturesView = StatusPicturesView()


}

// MARK: - 设置界面
extension StatusOriginalView{

    private func setupUI(){
        //设置背景颜色
        backgroundColor = UIColor.whiteColor()

        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picturesView)
        
        let margin: CGFloat = 12.0
        
        // 测试代码
        let count = random() % 20 + 1
        var str = ""
        for _ in 0..<count {
            str += "新浪微博"
        }
        contentLabel.text = str
        
        // 测试头像
        //iconView.image = UIImage(named: "avatar_default_big")?.cz_asyncDrawImage(imageSize, isCorner: true)
        UIImage(named: "avatar_default_big")?.cz_asyncDrawImage(CZStatusIconSize, isCorner: true, finished: { (image) -> () in
            self.iconView.image = image
        })
        
        //自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(margin)
            make.left.equalTo(margin)
            make.size.equalTo(CZStatusIconSize)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        
        memberIconView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }
    
        vipIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_right).offset(-4)
            make.centerY.equalTo(iconView.snp_bottom).offset(-4)
            
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
    
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(margin)
        }
    
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.right.equalTo(self).offset(-margin)
            
        }
        picturesView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            
            //默认不设置图片视图大小
            make.size.equalTo(CGSizeZero)
            
            // 很重要，设置底部约束，能够自动计算高度
            make.bottom.equalTo(self).offset(-margin)
            
        }
    }
}


